package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.JsonResult;
import com.gxa.pojo.CareRemind;
import com.gxa.service.CustomerService;
import com.gxa.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/customer_care/care_remind/update")
public class CareRemindUpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Long customer_code = Long.parseLong(request.getParameter("customer_code"));
        String customer_name = request.getParameter("customer_name");
//        System.out.println(customer_code + " " + customer_name);
        String remind_name = request.getParameter("remind_name");
        String remind_time = request.getParameter("remind_time");
        String remind_type = request.getParameter("remind_type");
        String[] remindCycles = request.getParameterValues("remind_cycle");
        //获取提醒频率，以二进制样式存储，存入数组并转为字符串
        int[] bin = {0,0,0,0,0,0,0};
        for (String remindCycle : remindCycles) {
            switch (remindCycle) {
                case "周一":
                    bin[1]=1;
                    break;
                case "周二":
                    bin[2]=1;
                    break;
                case "周三":
                    bin[3]=1;
                    break;
                case "周四":
                    bin[4]=1;
                    break;
                case "周五":
                    bin[5]=1;
                    break;
                case "周六":
                    bin[6]=1;
                    break;
                case "周日":
                    bin[7]=1;
                    break;
            }
        }
        String remind_cycle = ""+bin[0]+bin[1]+bin[2]+bin[3]+bin[4]+bin[5]+bin[6];
        System.out.println(remind_cycle);
        String remind_rate = request.getParameter("remind_rate");
        String remark = request.getParameter("remark");
        CareRemind careRemind = new CareRemind();
        careRemind.setCustomer_code(customer_code);
        careRemind.setCustomer_name(customer_name);
        careRemind.setRemind_name(remind_name);
        careRemind.setRemind_time(remind_time);
        careRemind.setRemind_type(remind_type);
        careRemind.setRemind_cycle(remind_cycle);
        careRemind.setRemind_rate(remind_rate);
        careRemind.setRemark(remark);
        //调用service
        CustomerService customerService = new CustomerServiceImpl();
        JsonResult jsonResult = customerService.updateCustomerCareRemind(careRemind);
        //响应jsonResult
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(jsonResult));
    }
}
